﻿Imports TMySQL
Public Class frmExplain
    Implements ITabForm
    Private _SQL As String
    Private MyExplain As TMyExplainPlan
    Private Const COLS As Integer = 3

    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        MyExplain = New TMyExplainPlan(frmMain.Connection)

        frmMain.GridSettings.ApplyAll(Me)
        frmMain.EditorSettings.ApplyAll(Me)
        frmMain.GeneralSettings.ApplyAll(Me)
    End Sub

    Public Property SQL() As String
        Get
            Return _SQL
        End Get
        Set(ByVal value As String)
            _SQL = value
            Reload()
        End Set
    End Property

    Public Sub Reload()
        MyExplain.SQL = _SQL
        txtSQL.Text = _SQL
        MyExplain.Explain()
        Grid.DataSource = MyExplain.DataTable
        Common.SetGridAllowSorting(Grid, DataGridViewColumnSortMode.NotSortable)
        ShowPlan()
    End Sub

    Private Sub ShowPlan()
        Dim currentTop As Integer = 0
        Dim maxHeight As Integer = 0
        pnl.Controls.Clear()
        For i As Integer = 0 To MyExplain.Count - 1
            If i Mod 3 = 0 Then
                currentTop = currentTop + maxHeight
                maxHeight = 0
            End If
            Dim frmRet As New frmExplainItem
            frmRet.TopLevel = False
            frmRet.Parent = pnl
            frmRet.ExplainItem = MyExplain(i)

            frmRet.Left = (i Mod 3) * frmRet.Width + 3
            frmRet.Top = currentTop + 3
            If maxHeight < frmRet.Height Then
                maxHeight = frmRet.Height
            End If

            frmRet.Show()
        Next
    End Sub

    Protected Overrides Sub Finalize()
        If MyExplain IsNot Nothing Then
            MyExplain.Dispose()
        End If
        MyBase.Finalize()
    End Sub
End Class